package com.zzmcc.jms.controller;

import com.jfinal.aop.Before;
import com.jfinal.ext.render.excel.JxlsRender;
import com.jfinal.kit.PathKit;
import com.jfinal.log.Logger;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.zzmcc.base.BaseController;
import com.zzmcc.common.AuthorizeInterceptor;
import com.zzmcc.jms.model.Project;
import com.zzmcc.jms.model.ProjectInspect;
import com.zzmcc.sys.model.Userinfo;
import util.ChristStringUtil;
import util.ModelUtils;
import util.StringUtil;

import java.text.SimpleDateFormat;
import java.util.*;

import static com.zzmcc.jms.common.ModelConfigWms.TABLE_Project_Build;

public class ProjectInspectController extends BaseController {
    private static final Logger log =  Logger.getLogger(ProjectInspectController.class);
	@Before(AuthorizeInterceptor.class)
	public void index() {
		StringBuffer whee = new StringBuffer();
		List<Object> param = new ArrayList<Object>();

		String name = getPara("project.name");
		String owner = getPara("project.owner");
		String homeCounty = getPara("project.homeCounty");
		String isDesign = getPara("project.isDesign");
		String isStart = getPara("project.isStart");
		String isComplete = getPara("project.isComplete");
		String isPass = getPara("project.isPass");
		String modifyDate = getPara("project.modifyDate");
		if (ChristStringUtil.isNotEmpty(name))
		{
			whee.append(" and name like ?");
			param.add("%" + name + "%");
		}
		if (ChristStringUtil.isNotEmpty(owner))
		{
			whee.append(" and owner like ?");
			param.add("%" + owner + "%");
		}
		if (ChristStringUtil.isNotEmpty(homeCounty))
		{
			whee.append(" and homeCounty = ?");
			param.add(homeCounty);
		}
		if (ChristStringUtil.isNotEmpty(isDesign))
		{
			whee.append(" and coalesce(b.isDesign,'否') = ?");
			param.add(isDesign);
		}
		if (ChristStringUtil.isNotEmpty(isStart))
		{
			whee.append(" and coalesce(b.isStart,'否') = ?");
			param.add(isStart);
		}
		if (ChristStringUtil.isNotEmpty(isComplete))
		{
			whee.append(" and coalesce(b.isComplete,'否') = ?");
			param.add(isComplete);
		}
		if (ChristStringUtil.isNotEmpty(isPass))
		{
			whee.append(" and coalesce(b.isPass,'否') = ?");
			param.add(isPass);
		}
		if (ChristStringUtil.isNotEmpty(modifyDate))
		{
			whee.append(" and TO_DAYS(modifyDate) >= TO_DAYS(?)");
			param.add(modifyDate);
		}
		Page<ProjectInspect> pager = ProjectInspect.dao.paginate(
				getParaToInt("pageNum",1),
				getParaToInt("numPerPage", 20),
				"select a.id,a.homeCounty,a.name,a.code,a.longitude,a.latitude,a.addr,a.projectDate,b.isDesign,b.designDate,b.isStart,b.startDate,b.isComplete,b.completeDate,b.isPass,b.passDate,b.isBaseStart,b.baseStartDate,b.isBaseComplete,b.baseCompleteDate,b.isAuxStart,b.auxStartDate,b.isAuxComplete,b.auxCompleteDate,b.isElectStart,b.electStartDate,b.isElectComplete,b.electCompleteDate,b.modifyDate",
				"from jms_project a left join jms_project_inspect b on b.pid=a.pid  " +
						"where isSelectConfirm='是' " + whee.toString() + "order by b.modifyDate desc",
				param.toArray());
		setAttr("pager", pager);
		setAttr("name", name);
		setAttr("owner", owner);

		setAttr("homeCounty", homeCounty);
		setAttr("isDesign", isDesign);
		setAttr("isStart", isStart);
		setAttr("isComplete", isComplete);
		setAttr("isPass", isPass);

		setAttr("modifyDate", modifyDate);
		render("../project_inspect_list.html");
	}
	public void edit() {

		String id = getPara("id");
		Userinfo loginUser =getSessionAttr("loginUser");
		String modifyUser=loginUser.get("realname");
		int groupid=loginUser.get("groupid");

		String sql = "select a.name,a.pid,a.code,b.id,b.isDesign,b.designDate,b.isStart,b.startDate,b.isComplete,b.completeDate,b.isPass, b.isElectStart,b.isElectComplete,b.isBaseStart,b.isBaseComplete,b.isAuxStart,b.isAuxComplete,b.isBody,b.isWireless ,b.modifyMemo,b.memo " +
				"from jms_project a left join jms_project_inspect b on b.pid=a.pid  where a.id=?";

		ProjectInspect m = ProjectInspect.dao.findFirst(sql, id);
		setAttr("projectInspect",m);
		setAttr("modifyUser", modifyUser);
		setAttr("groupid",groupid);
		render("../project_inspect_input.html");
	}


	public void update()
	{
		try
		{
			ProjectInspect m = getModel(ProjectInspect.class, "projectInspect");

			String isBaseStart = m.getStr("isBaseStart");
			if ((StringUtil.isNotEmpty(isBaseStart)) && (isBaseStart.equals("是"))) {
				m.set("baseStartDate", new Date());
			}
			String isBaseComplete = m.getStr("isBaseComplete");
			if ((StringUtil.isNotEmpty(isBaseComplete)) && (isBaseComplete.equals("是"))) {
				m.set("baseCompleteDate", new Date());
			}
			String isAuxStart = m.getStr("isAuxStart");
			if ((StringUtil.isNotEmpty(isAuxStart)) && (isAuxStart.equals("是"))) {
				m.set("auxStartDate", new Date());
			}
			String isAuxComplete = m.getStr("isAuxComplete");
			if ((StringUtil.isNotEmpty(isAuxComplete)) && (isAuxComplete.equals("是"))) {
				m.set("auxCompleteDate", new Date());
			}
			String isElectStart = m.getStr("isElectStart");
			if ((StringUtil.isNotEmpty(isElectStart)) && (isElectStart.equals("是"))) {
				m.set("electStartDate", new Date());
			}
			String isElectComplete = m.getStr("isElectComplete");
			if ((StringUtil.isNotEmpty(isElectComplete)) && (isElectComplete.equals("是"))) {
				m.set("electCompleteDate", new Date());
			}
			String isComplete = m.getStr("isComplete");
			if ((StringUtil.isNotEmpty(isComplete)) && (isComplete.equals("是"))) {
				m.set("completeDate", new Date());
			}
			String isPass = m.getStr("isPass");
			if ((StringUtil.isNotEmpty(isPass)) && (isPass.equals("是"))) {
				m.set("passDate", new Date());
			}
			ModelUtils.fillForUpdate(m);
			m.update();
			renderDWZSuccessJson("修改成功！");
		}
		catch (Exception e)
		{
			renderDWZSuccessJson("设计未完成，不能更新进度！");
		}
	}


	public void save() {
		try{
			ProjectInspect m = getModel(ProjectInspect.class, "projectInspect");
			ModelUtils.fillForUpdate(m);
			m.save();
			renderDWZSuccessJson("添加成功！");
		}catch (Exception e){
			renderDWZSuccessJson("重复提交，失败！");
		}
	}

	public void editUnit()
	{
		setAttr("project", Project.dao.findById(getPara("id")));
		render("../project_unit_input.html");
	}

	public void updateUnit() {
		Project s = getModel(Project.class, "project");
		s.update();
		renderDWZSuccessJson("修改成功！");
	}

	public void export()
	{
		StringBuffer whee = new StringBuffer();
		List<Object> param = new ArrayList();

		String homeCounty = getPara("project.homeCounty");
		String owner = getPara("project.owner");
		String isCm = getPara("project.isCm");
		String isCu = getPara("project.isCu");
		String isCt = getPara("project.isCt");
		String modifyDate = getPara("project.modifyDate");
		if (ChristStringUtil.isNotEmpty(owner))
		{
			whee.append(" and owner like ?");
			param.add("%" + owner + "%");
		}
		if (ChristStringUtil.isNotEmpty(homeCounty))
		{
			whee.append(" and homeCounty = ?");
			param.add(homeCounty);
		}
		if (ChristStringUtil.isNotEmpty(isCm))
		{
			whee.append(" and isCm = ?");
			param.add(isCm);
		}
		if (ChristStringUtil.isNotEmpty(isCt))
		{
			whee.append(" and isCt = ?");
			param.add(isCt);
		}
		if (ChristStringUtil.isNotEmpty(isCu))
		{
			whee.append(" and isCu = ?");
			param.add(isCu);
		}
		if (ChristStringUtil.isNotEmpty(modifyDate))
		{
			whee.append(" and TO_DAYS(modifyDate) >= TO_DAYS(?)");
			param.add(modifyDate);
		}
		String sql = "select a.id,a.pid,a.homeCounty,a.name,a.code,a.auxUnit,a.buildUnit,a.projectOwner,b.isDesign,b.DesignDate,b.isStart,b.startDate,b.isComplete,b.completeDate,b.isPass,b.passDate,b.isBaseStart,b.baseStartDate,b.isBaseComplete,b.baseCompleteDate,b.isAuxStart,b.auxStartDate,b.isAuxComplete,b.auxCompleteDate,b.isElectStart,b.electStartDate,b.isElectComplete,b.electCompleteDate,b.modifyDate,b.modifyMemo " +
				"from jms_project a \n" +
				"left join jms_project_inspect b\n" +
				"on a.pid=b.pid \n" +
				"where isSelectConfirm='是' " + whee.toString() + "order by b.modifyDate desc";

		List<Record> buildDetail = Db.find(sql, param.toArray());

		Map<String, Object> beans = new HashMap();
		beans.put("buildDetail", buildDetail);

		String templateFileName = PathKit.getRootClassPath() + "/tpl_project_inspect.xls";
		String filename = "project_inspect.xls";
		render(JxlsRender.me(templateFileName).filename(filename).beans(beans));
	}

	public void trans()
	{
		String[] ids = getParaValues("ids");
		for (String id : ids) {
			try
			{
				String sql = "select a.pid,b.id,coalesce(b.isDesign,'否') isDesign from jms_project a left join jms_project_inspect b on b.pid=a.pid  where a.id=?";
				ProjectInspect m = ProjectInspect.dao.findFirst(sql,id);
				if (null == m.get("id"))
				{
					m.set("isDesign", "是");
					m.set("designDate", new Date());
					ModelUtils.fillForUpdate(m);
					m.save();
				}
				else
				{
					String isDesign = m.getStr("isDesign");
					if (isDesign.equals("否"))
					{
						m.set("isDesign", "是");
						m.set("designDate", new Date());
					}
					ModelUtils.fillForUpdate(m);
					m.update();
				}
			}
			catch (Exception e)
			{
				renderDWZSuccessJson("设计完成，不能修改！");
				return;
			}
		}
		renderDWZSuccessJson("更新成功!");
	}

	@Before(AuthorizeInterceptor.class)
	public void delete() {

		String[] ids = getParaValues("ids");
		for(String id:ids){
			Project m =Project.dao.findById(id);
			Db.update("delete a from jms_project_inspect a where pid =?", m.get("pid"));
		}
		renderDWZSuccessJson("删除成功!");

	}

}
